<template>
    <div class="nav-menu">
      <div class="user-info" v-if="authStore.isAuthenticated">
        <el-dropdown>
          <div class="user-avatar">
            <el-avatar :size="36" :src="authStore.userAvatar" />
            <span class="user-name">{{ authStore.user.nickname || authStore.user.username }}</span>
            <el-icon><arrow-down /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="router.push('/profile')">
                <el-icon><user /></el-icon>个人中心
              </el-dropdown-item>
              <el-dropdown-item divided @click="handleLogout">
                <el-icon><switch-button /></el-icon>退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <div v-else>
      <el-button link @click="router.push('/login')">登录</el-button>
      <el-button link @click="router.push('/register')">注册</el-button>
        </div>
    </div>
  </template>
  
  <script setup>
  import { useAuthStore } from '@/stores/user'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import {
    User,
    SwitchButton,
    ArrowDown
  } from '@element-plus/icons-vue'
  
  const authStore = useAuthStore()
  const router = useRouter()
  
  const handleLogout = () => {
    authStore.logout()
    ElMessage.success('已退出登录')
    router.push('/login') // 登出后跳转到登录页
  }
  </script>
  
  <style scoped lang="scss">
  .nav-menu {
    display: flex;
    align-items: center;
    height: 100%;
  
    .user-info {
      height: 100%;
      display: flex;
      align-items: center;
      padding: 0 10px;
      cursor: pointer;
      transition: background-color 0.3s;
  
      &:hover {
        background-color: rgba(255, 255, 255, 0.1);
      }
  
      .user-avatar {
        display: flex;
        align-items: center;
        gap: 8px;
  
        .user-name {
          color: white;
          font-size: 14px;
          max-width: 120px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
  
        .el-icon {
          color: white;
          font-size: 12px;
        }
      }
    }
  
    .el-button {
      color: white;
      padding: 0 12px;
      background: transparent;
      border: none;
  
      &:hover {
        color: var(--el-color-primary-light-3);
        background: transparent;
      }
  
      &:focus {
        background: transparent;
      }
    }
  }
  </style>